﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.Model;
using Touch2Like.DAL;
using Touch2Like.SharedLibrary;

namespace Touch2Like.Web.Admin.Popup
{
    public partial class AddGoods : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["ListID"]))
                {
                    ProductDAL goods = new ProductDAL();
                    ProductInfo info = goods.GetProductsBySKU(Convert.ToInt32(Request["ListID"]));

                    if (info != null)
                    {
                        tb_GoodsName.Text = info.Name;
                        tb_GoodsDes.Text = info.Headline;
                        tb_GoodsPic.Text = info.Headline;
                        tb_GoodsPrice.Text = info.Price.ToString();
                        tb_GoodsCount.Text = info.Quantity.ToString();
                        tb_GoodsDescritpion.Text = info.Description;
                        tb_GoodsParameter.Text = info.Technical;
                    }
                }
            }
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            ProductDAL product = new ProductDAL();
            ProductInfo info = new ProductInfo();

            info.Name = tb_GoodsName.Text.Trim();
            info.Headline = tb_GoodsDes.Text.Trim();
            info.PictureLocation = tb_GoodsPic.Text.Trim();

            if (tb_GoodsPrice.Text.Trim() != "")
                info.Price = Convert.ToDecimal(tb_GoodsPrice.Text.Trim());
            if (tb_GoodsCount.Text.Trim() != "")
                info.Quantity = Convert.ToInt32(tb_GoodsCount.Text.Trim());

            info.Description = tb_GoodsDescritpion.Text.Trim();
            info.Technical = tb_GoodsParameter.Text.Trim();

            if (!string.IsNullOrEmpty(Request["ListID"]))
            {
                info.SKU = Convert.ToInt32(Request["ListID"]);
                product.UpdateProduct(info);
            }
            else
            {
                product.AddProduct(info);
            }

            ClientScript.RegisterStartupScript(this.GetType(), "update", "<script>top.document.location.href=top.document.location.href;top.document.location.reload();</script>");
        }
    }
}